package com.ruoyi.laboratory.controller;

import java.util.List;

import com.ruoyi.laboratory.domain.LabResearchProject;
import com.ruoyi.laboratory.domain.dto.LabResearchProjectDTO;
import com.ruoyi.laboratory.domain.vo.LabResearchProjectVO;
import com.ruoyi.laboratory.domain.excel.LabResearchProjectExcel;
import com.ruoyi.laboratory.service.ILabResearchProjectService;
import com.ruoyi.common.core.utils.exptimpt.ExImportContext;
import com.ruoyi.laboratory.enums.ExImportBusinessEnum;
import com.ruoyi.common.core.domain.R;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.utils.PageUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.web.controller.BaseController;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 科研项目Controller
 * 
 * @author liangh
 * @date 2024-11-07
 */
@RestController
@RequestMapping("/project")
public class LabResearchProjectController extends BaseController
{
    @Resource
    private ILabResearchProjectService labResearchProjectService;

    @Resource
    private ExImportContext<LabResearchProjectExcel> exImportContext;

    /**
     * 查询科研项目列表
     *
     * @return {@link TableDataInfo<LabResearchProjectVO>}
     */
    @RequiresPermissions("laboratory:project:list")
    @PostMapping("/list")
    public TableDataInfo<LabResearchProjectVO> list(@RequestBody LabResearchProjectDTO labResearchProjectDTO)
    {
        PageUtils.startPage(labResearchProjectDTO);
        List<LabResearchProject> list = labResearchProjectService.selectLabResearchProjectList(labResearchProjectDTO);
        return PageUtils.toTableData(list, LabResearchProjectVO.buildListByDO(list));
    }

    /**
     * 导出科研项目列表
     */
    @RequiresPermissions("laboratory:project:export")
    @Log(title = "科研项目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody LabResearchProjectDTO labResearchProjectDTO)
    {
        List<LabResearchProject> list = labResearchProjectService.selectLabResearchProjectList(labResearchProjectDTO);
        exImportContext.exportData(ExImportBusinessEnum.PROJECT, response, LabResearchProjectExcel.buildListByDO(list));
    }

    /**
     * 导入科研项目
     *
     * @return {@link R<Boolean>}
     */
    @RequiresPermissions("laboratory:project:import")
    @Log(title = "科研项目导入", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public R<Boolean> importExcel(HttpServletResponse response, MultipartFile file) throws Exception
    {
        return exImportContext.importData(ExImportBusinessEnum.PROJECT, response, file) < 0 ? null : R.ok();
    }

    /**
     * 科研项目下载模板
     */
    @RequiresPermissions("laboratory:project:export")
    @Log(title = "科研项目下载模板", businessType = BusinessType.EXPORT)
    @PostMapping("/downloadTemplate")
    public void downTemplate(HttpServletResponse response) {
        exImportContext.downImportTemplate(ExImportBusinessEnum.PROJECT, response);
    }

    /**
     * 获取科研项目详细信息
     *
     * @return {@link R<LabResearchProjectVO>}
     */
    @RequiresPermissions("laboratory:project:query")
    @GetMapping(value = "/{id}")
    public R<LabResearchProjectVO> getInfo(@PathVariable("id") String id)
    {
        return R.ok(LabResearchProjectVO.buildByDO(labResearchProjectService.selectLabResearchProjectById(id)));
    }

    /**
     * 新增科研项目
     *
     * @return {@link R<Integer>}
     */
    @RequiresPermissions("laboratory:project:add")
    @Log(title = "科研项目", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public R<Integer> add(@RequestBody LabResearchProjectDTO labResearchProjectDTO)
    {
        return R.ok(labResearchProjectService.insertLabResearchProject(labResearchProjectDTO));
    }

    /**
     * 修改科研项目
     *
     * @return {@link R<Integer>}
     */
    @RequiresPermissions("laboratory:project:edit")
    @Log(title = "科研项目", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public R<Integer> edit(@RequestBody LabResearchProjectDTO labResearchProjectDTO)
    {
        return R.ok(labResearchProjectService.updateLabResearchProject(labResearchProjectDTO));
    }

    /**
     * 删除科研项目
     *
     * @return {@link R<Integer>}
     */
    @RequiresPermissions("laboratory:project:remove")
    @Log(title = "科研项目", businessType = BusinessType.DELETE)
	@PostMapping("/deleteByIds")
    public R<Integer> remove(@RequestBody LabResearchProjectDTO labResearchProjectDTO)
    {
        return R.ok(labResearchProjectService.deleteLabResearchProjectByIds(labResearchProjectDTO.getIds()));
    }
}
